//
//  RCAirFlyListVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-9-7.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCAirFlyListVC.h"
#import "RCCustomTabBar.h"
#import "RCFilterHeaderItem.h"
#import "RCTimeItem.h"
#import "RCFlyCell.h"
#import "RCTicketOrderVC.h"
#import "RCTranlistDataModel.h"
#import "IManager+TrainList.h"
#import "RCTicketFlyOrderViewController.h"
#import "RCTrainOrderDetailInfoDataModel.h"

@interface RCAirFlyListVC ()<IManagerDelegate>{
    RCPlanelistDataModel *_model;
    NSInteger _timeSort ;
    NSInteger _priceSort;
    
}
@property (nonatomic, retain) RCPlanelistDataModel *model;
@property (nonatomic,retain) MBProgressHUDEx *progressHub;

@end

@implementation RCAirFlyListVC
@synthesize model = _model;
@synthesize progressHub = _progressHub;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _timeSort = 0;
        _priceSort = 0;
    }
    return self;
}

- (void) dealloc
{
    self.model = Nil;
    self.startTime = nil;
    self.startTrainPort = nil;
    self.endTrainPort = nil;
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.titleLabelText = [NSString stringWithFormat:@"%@-%@",self.startPortName,self.endPortName];
    self.navigationItem.titleView = [self createTitleView];
    
    RCCustomTabBar *tabBar = [[RCCustomTabBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44) tabBarTitles:[NSArray arrayWithObjects:@"起飞",@"价格", nil] andClass:[RCFilterHeaderItem class] andDelegate:self];
    [self.view addSubview:tabBar];
    [tabBar release];
    
    
    //    RCCustomTabBar *timeTabBar = [[RCCustomTabBar alloc] initWithFrame:CGRectMake(0, UIScreenHeight-20-44-30, 320, 30) tabBarTitles:[NSArray arrayWithObjects:@"前一天",@"今天",@"后一天", nil] andClass:[RCTimeItem class]];
    //    [self.view addSubview:timeTabBar];
    //    [timeTabBar release];
    
    baseVCTableView.frame = CGRectMake(0, 45, UIScreenHeight, UIScreenHeight-20-44-44);
    [self refreshData];
}


- (void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    
    [[IManager sharedInstance] requestPlaneListWithStartPort:self.startTrainPort withEndPort:self.endTrainPort withStartTime:self.startTime withTripType:1 withSeatType:self.seatTypeInt withTimesort:_timeSort withPricesort:_priceSort withDelegate:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.total;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"AirFlyListCell";
    RCFlyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (nil == cell) {
        cell = [[[RCFlyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }
    
    RCPlaneItemDataModel *item = [self.model.listItems objectAtIndex:[indexPath row]];
    [cell bindData:item];
    return cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    RCPlaneItemDataModel *item = [self.model.listItems objectAtIndex:[indexPath row]];
    if (nil == item) {
        return;
    }
    RCFlyOrderItemInfo *flyOrderInfo = [[[RCFlyOrderItemInfo alloc]init]autorelease];
    [flyOrderInfo initFlyOrderItemWithPlaneItem:item];
    flyOrderInfo.startDate = self.startTime;
    flyOrderInfo.startCity = self.startPortName;
    flyOrderInfo.endCity = self.endPortName;
    flyOrderInfo.startCityCode = self.startTrainPort;
    flyOrderInfo.endCityCode = self.endTrainPort;
    
    RCTicketFlyOrderViewController *vc = [[RCTicketFlyOrderViewController alloc] initWithFlyData:flyOrderInfo];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    [_progressHub removeFromSuperview];
    [_progressHub release];
    _progressHub = nil;
}


- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    [_progressHub removeFromSuperview];
    [_progressHub release];
    _progressHub = nil;
    
    self.model = (RCPlanelistDataModel *)dataModel;
    [baseVCTableView reloadData];
}


- (void)didSelectTabItem:(TabItem *)itemView
{
    if ([itemView.title isEqualToString:@"起飞"]) {
        if (_timeSort == 1) {
            _timeSort = 2;
        }else if(_timeSort == 2){
            _timeSort = 1;
        }else{
            _timeSort = 1;
        }
        _priceSort = 0;
    }else{
        if (_priceSort == 1) {
            _priceSort = 2;
        }else if(_priceSort == 2){
            _priceSort = 1;
        }else{
            _priceSort = 1;
        }
        _timeSort = 0;
    }
    [self refreshData];
}



@end
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    RCFlyCell *cell = [[RCFlyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tes"];
////    cell.textLabel.text = [cell description];
//    [cell bindData];
//    return cell;
//}
//
//
//- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    RCTicketOrderVC *vc = [[RCTicketOrderVC alloc] initWithType:TicketType_Air];
//    [self.navigationController pushViewController:vc animated:YES];
//    [vc release];
//}
//@end
